package cn.shyouchi.wuguanjia.android.api.hanvon;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import java.io.ByteArrayOutputStream;
import java.io.File;

import cn.shyouchi.wuguanjia.android.components.retrofit.YouChiConverterFactory;
import cn.shyouchi.wuguanjia.android.model.IdCardEntity;
import cn.shyouchi.wuguanjia.android.model.IdCardRequest;
import cn.shyouchi.wuguanjia.android.model.IdCardReverseEntity;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import rx.Observable;
import rx.functions.Func1;
import top.zibin.luban.Luban;


/**
 * Created by Eenie on 2016/10/12 at 15:12
 * Des:
 */

public class HanvonApi {
    private HanvonService mService;

    Context mContext;

    public HanvonApi(Context context) {
        this.mContext = context;
        Retrofit retrofit = new Retrofit.Builder()
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(YouChiConverterFactory.create())
                .baseUrl("http://api.hanvon.com/")
                .build();
        mService = retrofit.create(HanvonService.class);
    }


    public Observable<IdCardEntity> identidyIdCard(String imgPath) {
        return Luban.get(mContext)
                .load(new File(imgPath))
                .putGear(Luban.THIRD_GEAR)
                .asObservable()
                .flatMap(new Func1<File, Observable<IdCardEntity>>() {
                    @Override
                    public Observable<IdCardEntity> call(File file) {
                        return mService.identidyIdCard(new IdCardRequest("0.0.0.0", imageFileToBase64(file.getAbsolutePath())));
                    }
                });
    }

    public Observable<IdCardReverseEntity> identidyIdCardReverse(String imgPath) {
        return Luban.get(mContext)
                .load(new File(imgPath))
                .putGear(Luban.THIRD_GEAR)
                .asObservable()
                .flatMap(new Func1<File, Observable<IdCardReverseEntity>>() {
                    @Override
                    public Observable<IdCardReverseEntity> call(File file) {
                        return mService.identidyIdCardReverse(new IdCardRequest("0.0.0.0", imageFileToBase64(file.getAbsolutePath())));
                    }
                });
    }

    private String imageFileToBase64(String path) {
        Bitmap bm = setPic(path);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 50, baos); //bm is the bitmap object
        byte[] b = baos.toByteArray();
        String base64 = Base64.encodeToString(b, Base64.DEFAULT);
        return base64;
    }

    private Bitmap setPic(String path) {
        int targetW = 2000;
        int targetH = 1000;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;
        int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;
        Bitmap bitmap = null;
        bitmap = BitmapFactory.decodeFile(path, bmOptions);
        return bitmap;
    }


}
